/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.platformservice.messaging;

import org.proteusframework.core.util.Assert;
import org.proteusframework.platformservice.persistence.api.messagebean.IMessageBeanDescriptor;

import java.util.Properties;

/**
 * Minimal implementation of an <code>IMessageBean</code> instance. All <code>IMessageBean</code> implementations
 * <b>must</b> extend this base class.
 *
 * @param <T> Specific type of message bean represented
 * @author Tacoma Four
 */
public class MessageBean<T> implements IMessageBean<T>
{
    /**
     * Metadata describing the underlying message bean.
     */
    protected final Properties metadata;

    /**
     * Actual immutable underlying message bean.
     */
    protected final T bean;

    /**
     * Immutable descriptor that matches the attached message bean.
     */
    protected final IMessageBeanDescriptor descriptor;

    /**
     * Constructs a minimal message bean instance with the immutable bean and descriptor objects.
     *
     * @param bean       Non-null message bean, e.g. a POJO
     * @param descriptor Non-null message bean descriptor that describes the accompanying message bean
     */
    public MessageBean(T bean, IMessageBeanDescriptor descriptor)
    {
        this(bean, descriptor, null);
    }

    /**
     * Constructs a minimal message bean instance with the immutable bean and descriptor objects, decorated with
     * a set of properties describing the bean.
     *
     * @param bean       Non-null message bean, e.g. a POJO
     * @param descriptor Non-null message bean descriptor that desribes the accompanying message bean
     * @param metadata   Optional metadata about the message bean
     */
    public MessageBean(T bean, IMessageBeanDescriptor descriptor, Properties metadata)
    {
        Assert.parameterNotNull(bean, "Parameter 'bean' must not be null");
        Assert.parameterNotNull(descriptor, "Parameter 'descriptor' must not be null");

        this.bean = bean;
        this.descriptor = descriptor;

        if (null != metadata)
        {
            this.metadata = metadata;
        } else
        {
            this.metadata = new Properties();
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public IMessageBeanDescriptor getDescriptor()
    {
        return descriptor;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public T getBean()
    {
        return bean;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Properties getMetadata()
    {
        return metadata;
    }
}
